package com.wtwd.campus.attendancesend;

import com.wtwd.campus.attendancenetty.AttendanceChannelMap;
import com.wtwd.campus.common.Constants;
import com.wtwd.campus.entity.*;
import com.wtwd.campus.utils.UnicodeUtil;
import io.netty.channel.Channel;
import org.apache.commons.lang.StringUtils;


/**
 * @Author ldaoliang
 * @Date create in 18:06 2021/3/23
 * @Description
 */
public interface AttendanceSender {

    AttendanceCommand dealMessage(MqMessage mqMessage);

    default boolean send(AttendanceCommand command) {
        if (command == null) {
            return false;
        }
        String productNo = command.getProductNo();
        String content = command.getContent();
        Channel channel =  AttendanceChannelMap.get(Constants.ATTENDANCE_ONLINE_PRE.concat(productNo));
        if(channel == null){
            return false;
        }
        System.out.println("发送指令："+content);
        channel.writeAndFlush(content);
        return true;
    }

    /**
     * 判断考勤机上报上来的数据是否变化过
     *
     * @param judgeContent
     * @return
     */
    default String getXOR(String judgeContent) {
        Integer result = 0;
        for (int i = 0; i < judgeContent.length() / 2; i++) {
            String compare = judgeContent.substring(i * 2, i * 2 + 2);
            Integer compareInt = parseHexStr2Byte(compare);
            Integer a = result ^ compareInt;
            result = a;
        }
        return String.format("%04X",result);
    }

    /**
     * 将16进制转换为二进制（int）
     *
     * @param hexStr
     * @return
     */
    default Integer parseHexStr2Byte(String hexStr) {
        if (hexStr.length() < 1)
            return null;
        int high = Integer.parseInt(hexStr.substring(0, 1), 16);
        int low = Integer.parseInt(hexStr.substring(1, 2),
                16);
        return high * 16 + low;
    }

}
